package com.dzm.demo.myshop.web.admin.abstracts;

import com.dzm.demo.myshop.commons.dto.BaseResult;
import com.dzm.demo.myshop.commons.dto.PageInfo;
import com.dzm.demo.myshop.commons.persistence.BaseEntity;
import com.dzm.demo.myshop.commons.persistence.BaseService;
import com.dzm.demo.myshop.domain.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

public abstract class AbstractsBaseController<T extends BaseEntity,S extends BaseService<T>> {

    /**
     * 注入业务逻辑层
     */
    @Autowired
    protected S service;

    /**
     * 跳转列表页
     * @return
     */
    public abstract String list();

    /**
     * 跳转用户表单页
     * @return
     */
    public abstract String form();

    /**
     * 显示用户详情
     * @return
     */
    public abstract String detail();

    /**
     * 保存信息
     * @param t
     * @param model
     * @param redirectAttributes
     * @return
     */
    public abstract String save(T t, Model model, RedirectAttributes redirectAttributes);

    /**
     * 删除
     * @param ids
     * @return
     */
    public abstract BaseResult delete(String ids);

    /**
     * 分页查询
     * @param request
     * @param t
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<T> page(HttpServletRequest request, T t){
        String strDraw = request.getParameter("draw");
        String strStart = request.getParameter("start");
        String strLength = request.getParameter("length");

        int draw = strDraw == null ? 0 : Integer.parseInt(strDraw);
        int start = strStart == null ? 0 : Integer.parseInt(strStart);
        int length = strLength == null ? 10 : Integer.parseInt(strLength);

        // 封装 Datatables 需要的结果
        PageInfo<T> pageInfo = service.page(start, length, draw, t);
        return pageInfo;
    };
}
